<?php

namespace App\Library\Traits\Orm;

use App\Models\ShopOrderGoodsModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\DB;

trait ProductSpecOrmTrait
{
    public function buyCount(): BelongsTo
    {
        return $this->belongsTo(ShopOrderGoodsModel::class, 'ps_id', 'spec_id')
            ->leftJoin('shop_order', 'shop_order.order_id', 'shop_order_goods.order_id')
            ->where('buy_user_id', USERID)->where('order_buy_status', '<>', 3)->select([
                'spec_id',
                DB::raw('cast(sum(quantity) as double) as quantity'),
            ]);
    }
}
